package cn.com.bsoft.mobile.ui

import cn.com.bsoft.mobile.service.AppVersionService
import cn.com.bsoft.mobile.service.AppVersionUploadReceiver
import cn.net.xyan.blossom.webUI.utils.webApplicationContext
import com.github.vok.karibudsl.*
import com.vaadin.server.Page
import com.vaadin.shared.Position
import com.vaadin.shared.ui.MarginInfo
import com.vaadin.ui.*
import com.vaadin.ui.themes.ValoTheme
import org.springframework.web.context.WebApplicationContext
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.OutputStream


class DefaultAppVersionUploadReceiver(val applicationContext: WebApplicationContext, val archiveUpload: ArchiveUpload) : AppVersionUploadReceiver {

    var out: ByteArrayOutputStream? = null

    var fileName: String? = null

    var mimeType: String? = null

    fun submit() {
        val inputStream = ByteArrayInputStream(out?.toByteArray())

        val appVersionService = applicationContext.getBean(AppVersionService::class.java)

        appVersionService.submitVersion(fileName!!, mimeType!!, inputStream, archiveUpload.textArea?.value)

        Notification("上传成功", "$fileName 上传成功", Notification.Type.ASSISTIVE_NOTIFICATION).apply {
            styleName += " " + ValoTheme.NOTIFICATION_CLOSABLE
            position = Position.TOP_CENTER
            show(Page.getCurrent())
        }

        archiveUpload.bOK.isEnabled = false
        archiveUpload.title.isVisible = false
    }

    override fun receiveUpload(fileName: String?, mimeType: String?): OutputStream {
        out = ByteArrayOutputStream()

        return out!!
    }

    override fun uploadSucceeded(event: Upload.SucceededEvent?) {
        fileName = event?.filename!!

        mimeType = event?.mimeType!!

        archiveUpload.bOK.isEnabled = true

        archiveUpload.title.value = "已上传:$fileName"
        archiveUpload.title.isVisible = true
    }

}

class ArchiveUpload : VerticalLayout() {

    var textArea: TextArea? = null

    lateinit var bOK: Button

    var uploadReceiver: DefaultAppVersionUploadReceiver

    lateinit var title: Label

    init {
        isSpacing = false
        margin = MarginInfo(false, false, false, false)

        val self = this
        setSizeFull()

        uploadReceiver = DefaultAppVersionUploadReceiver(webApplicationContext!!, self)

        label("应用上传") {
            w = fillParent; styleName = ValoTheme.LABEL_HUGE
            expandRatio = 0.0f
        }

        horizontalLayout {
            upload {
                buttonCaption = "上传版本"
                alignment = Alignment.BOTTOM_LEFT

                receiver = uploadReceiver
                addSucceededListener(uploadReceiver)
                expandRatio = 0.0f
            }

            bOK = button("提交") {
                alignment = Alignment.BOTTOM_LEFT
                isEnabled = false
                onLeftClick {
                    uploadReceiver.submit()
                }
            }

            title = label {
                isVisible = false
                alignment = Alignment.BOTTOM_LEFT
            }
        }
        // verticalLayout {
        //  margin = MarginInfo(false, false, false, false)

        textArea = textArea("版本说明") {
            setSizeFull()
            expandRatio = 1.0f
        }

        // expandRatio = 1.0f
        // }

    }

}